package cn.edu.sjtu.flaw_detection.controller;

import cn.edu.sjtu.flaw_detection.dto.StateDTO;
import cn.edu.sjtu.flaw_detection.dto.TaskDTO;
import cn.edu.sjtu.flaw_detection.enums.ResultEnum;
import cn.edu.sjtu.flaw_detection.service.FlawDetectionService;
import cn.edu.sjtu.flaw_detection.util.ResultUtil;
import cn.edu.sjtu.flaw_detection.vo.ResultVO;
import cn.edu.sjtu.flaw_detection.vo.TaskVO;
import cn.edu.sjtu.flaw_detection.vo.TasksWithTypeVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author loumoon
 * @date 2019-12-01 19:16
 */
@RestController
@Slf4j
public class FlawDetectionController {

  private final FlawDetectionService flawDetectionService;

  public FlawDetectionController(@Qualifier("mock") FlawDetectionService flawDetectionService) {
    this.flawDetectionService = flawDetectionService;
  }

  @PostMapping("/api/offer")
  public ResultVO addToTaskQueue(@RequestBody TaskDTO taskDTO) {
    flawDetectionService.addTaskToPool(taskDTO);
    return ResultUtil.success();
  }

  // state of ecn
  @GetMapping(value = "/api/state")
  public ResultVO getStatus() {
    StateDTO stateDTO = flawDetectionService.queryECNState();
    return ResultUtil.success(stateDTO);
  }

  @GetMapping(
      value = "/api/state",
      params = {"processId", "taskId"})
  public ResultVO getStatus(String processId, String taskId) {
    StateDTO stateDTO = flawDetectionService.queryTaskState(processId, taskId);
    return ResultUtil.success(stateDTO);
  }

  @PostMapping("/api/input")
  public ResultVO inputData(@RequestBody JSONObject input) {
    // 接收来自人工任务的输入 这个输入可能是直接从前端输入 预留
    // 流程管理模块不调用该接口
    return ResultUtil.success();
  }

  @GetMapping("/api/tasks")
  public ResultVO listTaskQueue() {
    //    List<TaskVO> taskVOQueue = flawDetectionService.listTaskQueue();
    //    return ResultUtil.success(taskVOQueue);
    Map<Integer, List<TaskVO>> map = flawDetectionService.retTasksPool();
    return ResultUtil.success(TasksWithTypeVO.build(map));
  }

  // called by frontend
  @PostMapping("/api/start")
  public ResultVO startTask(@RequestBody TaskDTO taskDTO) {
    boolean ret = flawDetectionService.startTask(taskDTO);
    if (ret) {
      return ResultUtil.success();
    }
    return ResultUtil.error(ResultEnum.TASK_START_ERROR);
  }

  @PostMapping("/api/end")
  public ResultVO endTask(@RequestBody TaskDTO taskDTO) {
    boolean ret = flawDetectionService.endTask(taskDTO);
    if (ret) {
      return ResultUtil.success();
    }
    return ResultUtil.error(ResultEnum.TASK_START_ERROR);
  }

  /*磁粉探伤的状态检测接口*/
  @RequestMapping(value = "/craft", method = RequestMethod.GET)
  public ResultVO detectStatus() {
    //    return flawDetectionService.detectStatus();
    return ResultUtil.success();
  }

  /*开启磁粉探伤工艺的接口*/
  @RequestMapping(value = "/craft", method = RequestMethod.POST)
  public ResultVO initiate() {
    //    return flawDetectionService.initiate();
    return ResultUtil.success();
  }
}
